package com.rede.didiok.admin.restapi.admin;


import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.rede.didiok.admin.annotion.AuthorityVerify.AuthorityVerify;
import com.rede.didiok.admin.annotion.OperationLogger.OperationLogger;
import com.rede.didiok.common.annotion.AvoidRepeatableCommit.AvoidRepeatableCommit;
import com.rede.didiok.common.dto.rank.RankDto;
import com.rede.didiok.common.exception.ThrowableUtils;
import com.rede.didiok.common.feign.RankFeignClient;
import com.rede.didiok.common.validator.group.Delete;
import com.rede.didiok.common.validator.group.GetList;
import com.rede.didiok.common.validator.group.Insert;
import com.rede.didiok.common.validator.group.Update;

/**
 * 榜单表 RestApi
 *
 * @author Rede
 * @since 2023-09-08
 */
@Api(value = "榜单相关接口", tags = {"榜单相关接口"})
@RestController
@RequestMapping("/admin/rank")
@Slf4j
public class RankRestApi {

    @Autowired
    private RankFeignClient rankFeignClient;

    @AuthorityVerify
    @ApiOperation(value = "获取榜单列表", notes = "获取榜单列表", response = String.class)
    @PostMapping("/getList")
    public String getList(@Validated({GetList.class}) @RequestBody RankDto rankDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("获取榜单列表");
        return rankFeignClient.getRankPageList(rankDto);
    }

    @AvoidRepeatableCommit
    @AuthorityVerify
    @OperationLogger(value = "增加榜单")
    @ApiOperation(value = "增加榜单", notes = "增加榜单", response = String.class)
    @PostMapping("/add")
    public String add(@Validated({Insert.class}) @RequestBody RankDto rankDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("增加榜单");
        return rankFeignClient.addRank(rankDto);
    }

    @AuthorityVerify
    @OperationLogger(value = "编辑榜单")
    @ApiOperation(value = "编辑榜单", notes = "编辑榜单", response = String.class)
    @PostMapping("/edit")
    public String edit(@Validated({Update.class}) @RequestBody RankDto rankDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("编辑榜单");
        return rankFeignClient.editRank(rankDto);
    }

    @AuthorityVerify
    @OperationLogger(value = "批量删除榜单")
    @ApiOperation(value = "批量删除榜单", notes = "批量删除榜单", response = String.class)
    @PostMapping("/deleteBatch")
    public String delete(@Validated({Delete.class}) @RequestBody List<RankDto> rankDtoList, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("批量删除榜单");
        return rankFeignClient.deleteBatchRank(rankDtoList);
    }

    @AuthorityVerify
    @OperationLogger(value = "置顶榜单")
    @ApiOperation(value = "置顶榜单", notes = "置顶榜单", response = String.class)
    @PostMapping("/stick")
    public String stick(@Validated({Delete.class}) @RequestBody RankDto rankDto, BindingResult result) {
        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("置顶榜单");
        return rankFeignClient.stickRank(rankDto);
    }

    /**
     * 通过引用量排序榜单
     * 引用量就是所有的文章中，有多少使用了该榜单，如果使用的越多，该榜单的引用量越大，那么排名越靠前
     *
     * @return
     */
    @AuthorityVerify
    @OperationLogger(value = "通过引用量排序榜单")
    @ApiOperation(value = "通过引用量排序榜单", notes = "通过引用量排序榜单", response = String.class)
    @PostMapping("/rankSortByMemberCount")
    public String rankSortByMemberCount() {
        log.info("通过引用量排序榜单");
        return rankFeignClient.rankSortByMemberCount();
    }
}

